import 'package:flutter/services.dart';
import 'package:shop/common/shop_material.dart';
import 'package:shop/view/login/LoginIndex.dart';

class CodeLogin extends StatefulWidget {
  final String phone;
  CodeLogin({Key key, this.phone}) : super(key: key);
  @override
  _CodeLoginState createState() => _CodeLoginState();
}

class _CodeLoginState extends State<CodeLogin> {
  TextEditingController textEditingController;
  bool showClose = false;
  bool buttonColor = false;
  @override
  void initState() {
    super.initState();
    textEditingController = TextEditingController();
    textEditingController.text = widget.phone ?? "";
    showClose = widget.phone != "";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading:
            IconButton(icon: Icon(Icons.close), onPressed: () => route.pop()),
        elevation: 0.0,
      ),
      body: GestureDetector(
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: Container(
          color: Colors.white,
          padding: EdgeInsets.all(40.w),
          child: Stack(
            children: <Widget>[
              ListView(
                children: <Widget>[
                  SizedBox(height: 40.h),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        "欢迎登录赶马商城",
                        style: TextStyle(
                          fontSize: 42.s,
                          fontWeight: FontWeight.bold,
                        ),
                      )
                    ],
                  ),
                  SizedBox(height: 100.h),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        "+86",
                        style:
                            TextStyle(color: Color(0xFF333333), fontSize: 24.s),
                      ),
                      Icon(Icons.chevron_right, size: 28.s),
                      SizedBox(width: 10.w),
                      Expanded(
                          child: TextField(
                        controller: textEditingController,
                        maxLength: 11,
                        keyboardType: TextInputType.number,
                        inputFormatters: [
                          WhitelistingTextInputFormatter.digitsOnly
                        ],
                        style: TextStyle(fontSize: 36.s),
                        decoration: InputDecoration(
                            contentPadding: EdgeInsets.only(bottom: 6.h),
                            counterText: "",
                            border: InputBorder.none,
                            hintText: "请输入手机号",
                            hintStyle: TextStyle(fontSize: 36.s)),
                        onChanged: (str) {
                          if (this.mounted) {
                            setState(() {
                              showClose = str.length > 0;
                              buttonColor = str.length >= 11;
                            });
                          }
                        },
                      )),
                      showClose
                          ? InkWell(
                              child: Image.asset(
                                  "images/user/ic_sign in_delete.png",
                                  height: 30.s),
                              onTap: () {
                                textEditingController.text = "";
                                setState(() {
                                  showClose = false;
                                  buttonColor = false;
                                });
                              },
                            )
                          : Text("")
                    ],
                  ),
                  Divider(),
                  Text(
                    "未注册的手机号验证后自动创建赶马账户",
                    style: TextStyle(color: Color(0xFF666666), fontSize: 20.s),
                  ),
                  SizedBox(height: 50.h),
                  InkWell(
                    onTap: () {
                      if (buttonColor) {
                        FocusScope.of(context).requestFocus(FocusNode());
                        route.pushNamed(MyRoutes.sendCode,
                            arguments: textEditingController.text);
                      }
                    },
                    child: Container(
                      height: 80.h,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          color: buttonColor
                              ? Color(0xFF00C657)
                              : Color(0xFF6EE6A3),
                          borderRadius: BorderRadius.circular(15)),
                      child: Text(
                        "获取短信验证码",
                        style: TextStyle(color: Colors.white, fontSize: 32.s),
                      ),
                    ),
                  ),
                  SizedBox(height: 30.h),
                  InkWell(
                    onTap: () => route.pushReplacementNamed(MyRoutes.pwLogin,
                        arguments: textEditingController.text),
                    child: Text(
                      "密码登录",
                      style:
                          TextStyle(fontSize: 24.s, color: Color(0xFF333333)),
                    ),
                  )
                ],
              ),
              Positioned(
                  top: 1100.h,
                  width: 710.w,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[LoginIndex.xieYi()],
                  ))
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    textEditingController.dispose();
    super.dispose();
  }
}
